<template>
  <div class="layout">
    <v-plain-header/>
    <div class="container">
      <el-form class="f" :model="form" ref="form" label-width="120px" label-position="right" :rules="rules">
        <el-form-item label="手机号码" prop="phone">
          <el-input class="line-input" maxlength="11" v-model="form.phone" placeholder="请输入手机号码"></el-input>
        </el-form-item>
        <el-form-item label="手机验证码" prop="verificationCode">
          <el-input class="line-input" maxlength="6" v-model="form.verificationCode" placeholder="请输入手机验证码">
            <v-message-code slot="suffix" @click.native="getMessageCode" ref="messageCode" class="edit-el-intput-suffix"
                            :phoneNum="form.phone"></v-message-code>
          </el-input>
        </el-form-item>
        <el-form-item label="登录密码" prop="password">
          <el-input class="line-input" type="password" v-model="form.password" placeholder="请输入登录密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input class="line-input" type="password" v-model="form.confirmPassword"
                    placeholder="请再次输入登录密码"></el-input>
        </el-form-item>
      </el-form>
      <div class="opera forget-pass">
        <el-button type="primary" @click="checkForm">确认修改</el-button>
      </div>
    </div>
    <v-plain-footer/>
  </div>
</template>
<script lang="ts">
    import { Vue, Component, Prop } from 'vue-property-decorator'
    import vFormTitle from '@/components/form-title'
    import vMessageCode from '@/components/message-code'
    import md5 from 'md5'
    import vPlainHeader from '@/components/plain-header'
    import vPlainFooter from '@/components/plain-footer'

    @Component({
        components: {
            vFormTitle, vMessageCode, vPlainHeader, vPlainFooter
        }
    })

    export default class Base extends Vue {
      // @Prop() userInfo

      form = {
          phone: '',
          verificationCode: '',
          password: '',
          confirmPassword: ''
      }
      rules = {
          phone: [
              { required: true, message: '请输入手机号码', trigger: 'blur' },
              {
                  pattern: /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/,
                  message: '手机号码错误',
                  trigger: 'blur'
              }
          ],
          verificationCode: [
              {required: true, message: '请输入验证码', trigger: 'blur'}
          ],
          password: [
              {required: true, message: '请输入登录密码', trigger: 'blur'}
          ],
          confirmPassword: [
              {required: true, message: '请再次输入登录密码', trigger: 'blur'},
              {validator: this.testConfirmPassword, trigger: 'blur'}
          ]
      }
      testConfirmPassword (rule, value, cb) {
          if (this.form.confirmPassword === '') {
              cb(new Error('请先输入密码'))
          } else {
              if (value === this.form.confirmPassword) {
                  cb()
              } else {
                  cb(new Error('您两次输入的密码不一致,请重新输入'))
              }
          }
      }
      getMessageCode () {
          this.$refs.messageCode.start()
      }
      checkForm () {
          this.$refs.form.validate((valid) => {
              if (valid) {
                  this.submitForm(this.form)
              }
          })
      }
      async submitForm(form) {
          form = {
              ...form,
              password: md5(form.password).toLocaleUpperCase(),
              confirmPassword: md5(form.confirmPassword).toLocaleUpperCase()
          }
          console.log(form)
          let res: AjaxResponse = await this.$axios.post('g/gateway/member/resetpassword', form)
          this.$utils.tips(res)
          if (res.responseCode === 200) {
              this.$router.push('/login')
          }
      }



    }
</script>
<style lang="less" scoped>
  .forget-pass{
    text-align: center;
  }
</style>